moduleApp.controller("InvestmentController",function($rootScope,$scope,$location,$http,$timeout){
      $scope.dataQuery={
          riskLevel:"1",
          profitLevel:"1"
      }
     $scope.paginationConf = {
        currentPage: 1
     };
    var GetAllEmployee = function () {
        $scope.$emit("BUSY");
        $scope.dataQuery.pageNum=$scope.paginationConf.currentPage;
        $scope.dataQuery.categoryId=$scope.dataQuery.riskLevel+$scope.dataQuery.profitLevel+"";
        $http({
            method:"POST",
            url:$rootScope.basePath+'product/list.do',
            data: $.param($scope.dataQuery),
            headers:{
                'Content-Type':'application/x-www-form-urlencoded'
            },
            timeout: 60000
        })
            .success(function(data){
                $scope.$emit("NOTBUSY");
                if(data.respCode=="failure_session"){
                    //登录超时
                    $location.path('/index');
                }else{
                    if(data.status==0){
                        $scope.paginationConf.totalItems = data.data.total;
                        $scope.paginationConf.currentPage = data.data.pageNum;
                        $scope.paginationConf.itemsPerPage = data.data.pageSize;
                        if (data.data.total == 0) {
                            $scope.emptyData = true;
                        } else {
                            $scope.emptyData = false;
                            $scope.listData=  data.data.list;
                        }
                    }
                }
            })
            .error(function(data){
                alert(data.msg)
            })

    }

    /***************************************************************
     当页码发生变化时监控后台查询
     ***************************************************************/
    $scope.$watchGroup(['paginationConf.currentPage','dataQuery.riskLevel','dataQuery.profitLevel'], GetAllEmployee);
    $scope.showDetail=false;
     /*点击列表购买按钮*/
    $scope.showDetailClick=function(item){
         console.log(item);
        $scope.showDetail=true;
        $scope.DetailData=item;
    }
    $scope.payOrder={};
    $scope.$watch('payOrder.buyNumber', function(){
        if($scope.payOrder.buyNumber){
            $scope.expectedProfit=parseInt($scope.DetailData.investTime)/12*parseInt($scope.payOrder.buyNumber)*parseFloat($scope.DetailData.interest)*0.01;
            $scope.expectedProfit=$scope.expectedProfit.toFixed(2);
        }
    });
     /*点击加号*/
     $scope.addOrder=function(){
         if($scope.payOrder.buyNumber && parseInt($scope.payOrder.buyNumber)<=(parseInt($scope.DetailData.remainStock)-100)){
             $scope.payOrder.buyNumber=parseInt($scope.payOrder.buyNumber)+100;
         }else if(!$scope.payOrder.buyNumber){
             $scope.payOrder.buyNumber=100;
         }
     }
    /*点击减号*/
    $scope.reduceOrder=function(){
        if($scope.payOrder.buyNumber && parseInt($scope.payOrder.buyNumber)>100){
            $scope.payOrder.buyNumber=parseInt($scope.payOrder.buyNumber)-100;
        }
    }
    /*点击立即购买*/
    $scope.buyBtnClick=function(){
        if(!$scope.payOrder.buyNumber){
            return;
        }
        $http({
            method:"POST",
            url:$rootScope.basePath+'order/create.do',
            data: $.param({
                "productId":$scope.DetailData.id,
                "buyNumber":$scope.payOrder.buyNumber
            }),
            headers:{
                'Content-Type':'application/x-www-form-urlencoded'
            },
            timeout: 60000
        })
            .success(function(data){
                $scope.$emit("NOTBUSY");
                if(data.respCode=="failure_session"){
                    //登录超时
                    $location.path('/index');
                }else{
                    if(data.status==0){
                        $location.path('order').search({orderNo: data.data.orderNo,buyNumber:$scope.payOrder.buyNumber});
                    }
                }
            })
            .error(function(data){
                alert(data.msg)
            })
    }


})
